/*
 * AutismSiteMasterController.java
 *
 * Created on October 24, 2006, 3:59 PM
 */

package com.asaga.controller;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;

import com.asaga.tools.*;

/**
 *
 * @author jeremys
 * @version
 */
public class AutismSiteMasterController extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession session = request.getSession();
        String action = request.getParameter("action");
        String submit = request.getParameter("submit");
        String message = null;
        AutismValidationHandler avh = new AutismValidationHandler();
        
     /*  
        // Test Code 
        if(action.equals("login")){
            String username = (String)request.getParameter("username");
            if(username==null){
                username="George";
            }
            session.setAttribute("username",username);
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/portal.jsp");
            dispatcher.forward(request,response);
        }
      */
     
        if(action.equals("login")){
            boolean sessionflag = false;           
            sessionflag = avh.makeUserSession(request,session);
            if(sessionflag==false){
                //response.sendRedirect("login.jsp");
                //message = "Incorrect username or password";
                //session.setAttribute("message", message);
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.jsp?message=Incorrect Username Or Password");
                dispatcher.forward(request,response);
            }else{
                session.setAttribute("username",request.getParameter("username"));
                session.setAttribute("password",request.getParameter("password"));
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/portal.jsp");
                dispatcher.forward(request,response);                
            }
        }else{
            boolean sessionflag=false;
            sessionflag = avh.checkUserSession(session,response);
            if(sessionflag==false){
                session.invalidate();
                response.sendRedirect("login.jsp");
            }            
        }
        
        
        if (action.equals("register")){
            RegistrationHandler rh = new RegistrationHandler(request);
            session.setAttribute("registration_handler",rh);
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/confirmation.jsp");
            
            dispatcher.forward(request,response);
        }
        else if(action.equals("logoff")){
            session.invalidate();
            response.sendRedirect("login.jsp");
        }
        else if(action.equals("addevent")){
            CalendarHandler eh = new CalendarHandler(request);
            session.setAttribute("event", eh.getEvent());
            session.setAttribute("location", eh.getLocation());
            response.sendRedirect("verifyevent.jsp");
        }
        
        else if(action.equals("verifyevent")){
            CalendarHandler eh = new CalendarHandler();
            if(submit.equals("Yes")){
                eh.setEvent((Event_StubImpl)session.getAttribute("event"));
                eh.addEvent();
                response.sendRedirect("portal.jsp");
            }
            else{
                response.sendRedirect("addevent.jsp");
            }
        }
        else{
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
            dispatcher.forward(request, response);
        }

    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
